# -*- coding: utf-8 -*-
from django.http import HttpResponse

from django.template.loader import get_template
from django.template import Context

from django.db import connection

from db.models import Developer, Job
from django.core.context_processors import csrf
from django.http import QueryDict

from PostRequests import PostRequests

class JobOperations(PostRequests):
    def addJob(self, request):
        mlist = [];
        def addJobHandler(data):
            print 'JobOperations.addJobHandler: data=', data
            try:
                jobParent = Job.objects.get(id=int(data.get("parent")))
            except Job.DoesNotExist:
                print 'JobOperations.addJobHandler: parent job not found'
            jobName = data.get("name")
            #jobType = data.get("type")
            job = Job(name=jobName);
            job.save();

            mlist = [];
            developers = [];
            for developer in job.developers.all():
                developers.append({'id': developer.id, 'name': developer.name});
            try:
                parent = int(job.parent.id);
            except:
                parent = -1;
            mlist.append({'id': job.id, 'parent': parent, 'name': job.name, 'developers': developers});
            
            print 'JobOperations.addJobHandler: job added, mlist=', mlist

        self.receive(request, addJobHandler)
        return self.send(mlist);
    def removeJob(self, request):
        def removeJobHandler(data):
            print 'JobOperations.removeJobHandler: data=', data
            jobId = data.get("id")
            try:
                jb = Job.objects.get(id=jobId)
                jb.delete()
                print 'JobOperations.removeJobHandler: job removed'
            except Job.DoesNotExist:
                print 'JobOperations.removeJobHandler: job not found'
        self.receive(request, removeJobHandler)
        return self.send();
    def updateJob(self, request):
        def updateJobHandler(data):
            print 'JobOperations.updateJobHandler: data=', data
            jobId = int(data.get("id"))
            jobParentId = int(data.get("parent"))
            jobName = data.get("name")
            jobParent = False;
            try:
                jobParent = Job.objects.get(id=jobParentId)
            except Job.DoesNotExist:
                print 'JobOperations.updateJobHandler: parent job not found'
            try:
                jb = Job.objects.get(id=jobId)
                if(jobParent):
                    jb.parent = jobParent
                jb.name = jobName
                jb.save()
                print 'JobOperations.updateJobHandler: job updated'
            except Job.DoesNotExist:
                print 'JobOperations.updateJobHandler: job not found'
        self.receive(request, updateJobHandler)
        return self.send();

    def getJobs(self, request):
        def get_items():
            print 'JobOperations.getJobs: get_items'
            jobs_list = Job.objects.all()
            mlist = [];
            for job in jobs_list:
                developers = [];
                for developer in job.developers.all():
                    developers.append({'id': developer.id, 'name': developer.name});
                try:
                    parent = int(job.parent.id);
                except:
                    parent = -1;
                mlist.append({'id': job.id, 'parent': parent, 'name': job.name, 'developers': developers});
            return mlist;

            print 'JobOperations.getJobs: send'
        return self.send(get_items());
